package com.fb;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.fb.FileBrowserPage;
import com.dev.GUI;
import com.dev.MainActivity;

public class QueryForPick extends LinearLayout implements OnClickListener,OnLongClickListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	MainActivity ma;
	GUI.Item itAll;
	GUI.Item itReverse;
	GUI.Item itShut;
    public QueryForPick(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setBackground(new GUI.BorderDrawable());
		wlp.gravity=Gravity.TOP|Gravity.LEFT;
		wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
		itAll=new GUI.Item(ma);
		itAll.setOnClickListener(this);
		itAll.setText("全选");
		itReverse=new GUI.Item(ma);
		itReverse.setOnClickListener(this);
		itReverse.setText("反选");
		itShut=new GUI.Item(ma);
		itShut.setOnClickListener(this);
		itShut.setText("关闭选择");

		LinearLayout.LayoutParams itlp=new LinearLayout.LayoutParams(-1,ma.dialogTitleBarHeight);
		addView(itAll,itlp);
		addView(itReverse,itlp);
		addView(itShut,itlp);
	}
	FileBrowserPage fbp;
	public void show(FileBrowserPage fbp){
		this.fbp=fbp;
		wlp.width=ma.wp/2;
		wlp.height=-2;
		Rect rect=new Rect();
		fbp.box.getGlobalVisibleRect(rect);
		wlp.x=rect.left;
		wlp.y=rect.bottom;
		ma.wm.addView(this,wlp);
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
	@Override
	public void onClick(View view) {
		if(view==itAll){
			boolean[] selected=fbp.selected;
			for(int index=0;index<selected.length;index++){
				selected[index]=true;
			}
			fbp.dlv.invalidate();
			dismiss();
		}else if(view==itReverse){
			boolean[] selected=fbp.selected;
			for(int index=0;index<selected.length;index++){
				selected[index]=!selected[index];
			}
			fbp.dlv.invalidate();
			dismiss();
		}else if(view==itShut){
			fbp.selected=null;
			fbp.box.setVisibility(GONE);
			fbp.dlv.invalidate();
			dismiss();
		}
	}
	@Override
	public boolean onLongClick(View view) {
		return false;
	}
	Rect visibile=new Rect();
	@Override
	protected void onLayout(boolean changed,int l,int t,int r,int b){
		super.onLayout(changed,l,t,r,b);
		getGlobalVisibleRect(visibile);

	}
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int rx=(int)event.getX();
		int ry=(int)event.getY();
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				if(rx<visibile.left||rx>=visibile.right||ry<visibile.top||ry>=visibile.bottom){
					dismiss();
				}
		}
		return super.onTouchEvent(event);
	}
}
